M.remoteStaticPathEnable=false;

myDbconfig={
    //  "host"     : "127.0.0.1",
    //         "user"     : "root",
    //         "password" : "123456",
    //         "port"     : "3306",
       
     "database" : "miapi"
}
Db=M.getMySql(myDbconfig);
Db.doSql(`select 1`).then(d=>console.log(d))


//openPath列表
openPathList=["/login","/register"]


app.begin((req,res)=>{
    console.log("SSSSSSSSSS",req.url)
    let isOpenPath=false;
    if(req.isStaticRequest() && "/index.html" !=req.url){
        return;
    }
    for(let i=0;i<openPathList.length;i++){
        if(req.url.startsWith(openPathList[i])){
            isOpenPath=true;
            break;
        }
    }
    if(!isOpenPath){
        //携带cookie有对应session
        if(req.session &&  req.session.username){
      
        }else{
            res.alreadySend = true;
            app.redirect("/login.html",req,res)
            return;
        }
    }
 })

/**
 * 登陆
 */
app.post("/login",function (req,res) {
    let r= JSON.parse(req.body);
    req.session={username:r.username}
    res.send(M.result("ok"));
 });

  /**
   * 退出
   */
  app.get("/signOut",function (req,res) {
    req.session={}
    app.redirect("/login.html",req,res)
    return;
});


app.post("/doSql",async function (req,res) {
    try{      
        var rows= await Db.doSql(req.params.sql);
        res.send(M.result(rows));
      
    }catch (e){
        res.send(M.result(e,false));
    }
})